package com.chen.controller;

import com.chen.model.Contract;
import com.chen.model.Goods;
import com.chen.model.Invoice;
import com.chen.service.GoodsService;
import com.chen.service.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/invoice")
public class InvoiceController {
    @Autowired
    private InvoiceService invoiceService;

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/addInvoice")
    public void addInvoice(){
        Invoice invoice =new Invoice();
        Goods goods = new Goods();
        Contract contract = new Contract();
        goods.setGoods_id(1);
        contract.setContract_id(1);
        invoice.setContract(contract);
        invoice.setGoods(goods);
        invoice.setInvoice_state(0);
        invoice.setGoods_price(20);
        invoice.setGoods_number(20);
        invoice.setInvoice_time(new Date());
        System.out.println(invoice);
        invoiceService.addInvoice(invoice);
        System.out.println(invoice);
    }

    @RequestMapping("/findAllInvoice")
    public String findAllInvoice(Model model){
        List<Invoice> invoiceList;
        invoiceList = invoiceService.findAllInvoice();
        model.addAttribute("invoiceList",invoiceList);
        return "invoice-list";
    }

    @RequestMapping("/invoice-send")
    public String invoiceSend(Integer invoice_id,Model model){
        Invoice invoice = invoiceService.findInvoiceById(invoice_id);
        Integer goods_id = invoice.getGoods().getGoods_id();
//        Goods goods = goodsService.findGoodsById(goods_id);
        model.addAttribute("invoice",invoice);
        model.addAttribute("goods_id",goods_id);
//        model.addAttribute("goods",goods);
        return "invoice-send";
    }

    @RequestMapping("/invoice-tosend")
    public String invoiceToSend(Integer invoice_id,Model model){
        Invoice invoice = invoiceService.findInvoiceById(invoice_id);
//        Integer goods_id = invoice.getGoods().getGoods_id();
        Goods goods = goodsService.findGoodsById(invoice.getGoods().getGoods_id());
        model.addAttribute("invoice",invoice);
        model.addAttribute("goods",goods);
        return "invoice-tosend";
    }
    @RequestMapping("/toSendGoods")
    public String toSendGoods(Invoice invoice, HttpServletRequest request){
        int option = Integer.parseInt(request.getParameter("option"));
        if(option==0){
            return "unpaid";
        }
        Invoice  invoice1 = invoiceService.findInvoiceById(invoice.getInvoice_id());
        invoice1.setInvoice_state(1);
        invoiceService.toSendInvoice(invoice1);
        return "success";
    }

    @RequestMapping("/sendGoods")
    public String sendGoods(Invoice invoice, HttpServletRequest request){
        Invoice  invoice1 = invoiceService.findInvoiceById(invoice.getInvoice_id());
        Integer goods_id = Integer.parseInt( request.getParameter("goods_id"));
        System.out.println(goods_id);
        Goods goods = goodsService.findGoodsById(goods_id);
        if(invoice1.getGoods_number()>goods.getGoods_number())
            return "goodsNumber-error";
//        System.out.println(invoice.getGoods_number());
        Integer goods_number = goods.getGoods_number()-invoice1.getGoods_number();
        goods.setGoods_number(goods_number);
//        System.out.println(invoice.getInvoice_id());
        goodsService.updateGoods(goods);
        invoice.setInvoice_state(2);
        invoiceService.sendInvoice(invoice);
        return "success";
    }
}
